package com.landeb.shop.huo.act;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.landeb.shop.R;
import com.landeb.shop.huo.ActControler;
import com.landeb.shop.huo.CacheDataManager;
import com.landeb.shop.huo.MyApplication;
import com.landeb.shop.huo.bean.BeanStatusInfo;
import com.landeb.shop.huo.httpRequest.UserConfirmationRequest;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class SettingActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.act_setting_ll_reset_profile)
    LinearLayout mActSettingLlResetProfile;
    @BindView(R.id.act_setting_ll_reset_pay_password)
    LinearLayout mActSettingLlResetPayPassword;
    @BindView(R.id.act_setting_ll_reset_login_password)
    LinearLayout mActSettingLlResetLoginPassword;
    @BindView(R.id.act_setting_ll_reset_mobile)
    LinearLayout mActSettingLlResetMobile;
    @BindView(R.id.act_setting_ll_del_cache)
    LinearLayout mActSettingLlDelCache;
    @BindView(R.id.act_setting_ll_about_version)
    LinearLayout mActSettingLlAboutVersion;
    @BindView(R.id.act_setting_ll_about_landeb)
    LinearLayout mActSettingLlAboutLandeb;
    @BindView(R.id.act_setting_tv_cache_size)
    TextView mActSettingTvCacheSize;
    private Button btnLogout, btnMore;
    private LinearLayout btnBack;

    private String mUrl;
    private Retrofit mRetrofit;
    private UserConfirmationRequest mRequest;

    public static void toAct(Context context) {
        Intent intent = new Intent(context, SettingActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);

        OkHttpClient client = MyApplication.getClient();
        mUrl = MyApplication.BASE_URL;
        mRetrofit = new Retrofit.Builder()
                .baseUrl(mUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(client)
                .build();
        mRequest = mRetrofit.create(UserConfirmationRequest.class);

        initView();
        initData();
        initEvent();

    }

    private void initEvent() {
        btnBack.setOnClickListener(this);
        btnMore.setOnClickListener(this);
        btnLogout.setOnClickListener(this);

        mActSettingLlResetProfile.setOnClickListener(this);
        mActSettingLlResetPayPassword.setOnClickListener(this);
        mActSettingLlResetMobile.setOnClickListener(this);
        mActSettingLlResetLoginPassword.setOnClickListener(this);
        mActSettingLlDelCache.setOnClickListener(this);
        mActSettingLlAboutVersion.setOnClickListener(this);
        mActSettingLlAboutLandeb.setOnClickListener(this);
    }


    private void initData() {

    }


    private void initView() {
        btnLogout = (Button) findViewById(R.id.act_setting_btn_logout);
        btnBack = (LinearLayout) findViewById(R.id.act_setting_btn_back);
        btnMore = (Button) findViewById(R.id.act_setting_btn_more);

        resetCache();
    }

    private void resetCache(){
        String cacheSize = "";
        try {
            cacheSize = CacheDataManager.getTotalCacheSize(SettingActivity.this);
        } catch (Exception e) {
            e.printStackTrace();
        }

        mActSettingTvCacheSize.setText(cacheSize.trim());
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.act_setting_btn_logout:
                // TODO: 2016/11/17 登出

                final ProgressDialog dialog = new ProgressDialog(this, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
                dialog.setCancelable(true);
                dialog.setMessage("注销中");
                dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                dialog.show();

                mRequest.logout()
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Subscriber<BeanStatusInfo>() {
                            @Override
                            public void onCompleted() {
                                dialog.dismiss();
                            }

                            @Override
                            public void onError(Throwable e) {
                                dialog.dismiss();
                            }

                            @Override
                            public void onNext(BeanStatusInfo beanStatusInfo) {

                                if (beanStatusInfo.getStatus() == 1) {

                                    SharedPreferences mPreferences = getSharedPreferences("user", Context.MODE_PRIVATE);
                                    SharedPreferences.Editor editor = mPreferences.edit();
                                    editor.remove("token");
                                    editor.remove("userData");
                                    editor.commit();

                                    MyApplication.resetClient("null");
                                    ActControler.removeAllAct();

                                    MainActivity.toAct(SettingActivity.this);

                                } else {
                                    Toast.makeText(SettingActivity.this, beanStatusInfo.getInfo(), Toast.LENGTH_SHORT).show();
                                }

                            }
                        });


                break;
            case R.id.act_setting_btn_back:
                // TODO: 2016/11/17 返回
                onBackPressed();
                break;
            case R.id.act_setting_btn_more:
                // TODO: 2016/11/17 不知道干嘛的
                break;

            case R.id.act_setting_ll_reset_profile:
                // TODO: 2017/8/24 修改资料
                ProfileEditActivity.toAct(SettingActivity.this);
                break;
            case R.id.act_setting_ll_reset_pay_password:
                // TODO: 2017/8/24 修改支付密码
                break;
            case R.id.act_setting_ll_reset_login_password:
                // TODO: 2017/8/24 修改登录密码
                PasswordChangeActivity.toAct(SettingActivity.this);
                break;
            case R.id.act_setting_ll_reset_mobile:
                // TODO: 2017/8/24 修改手机号
                break;
            case R.id.act_setting_ll_del_cache:
                // TODO: 2017/8/24 清除缓存
                CacheDataManager.clearAllCache(SettingActivity.this);
                resetCache();
                break;
            case R.id.act_setting_ll_about_version:
                // TODO: 2017/8/24 版本信息
                VersionActivity.toAct(this);
                break;
            case R.id.act_setting_ll_about_landeb:
                // TODO: 2017/8/24 关于天地网
                break;
        }
    }


}
